package cn.qingyun.gis.modules.init.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @Author zhoudeli
 * @Date 2021/8/19 8:09
 * @Description 时间工具类
 * @ClassName DateTimeUtil
 **/
public class DateTimeUtil {
    public static String formatTimeGmt(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss 'GMT'Z", Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return sdf.format(date);
    }

    public static String formatTime(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return sdf.format(date);
    }

    public static String format(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return sdf.format(date);
    }

    // 将"yyyy-MM-dd'T'HH:mm:ss'Z'"格式的时间转换成东八区的"yyyy-MM-dd HH:mm:ss"
    public static String formatTimeTZ(String time) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
            Date date = formatter.parse(time);
            Date date2 = new Date(date.getTime() + 8 * 3600 * 1000);
            return format(date2, "yyyy-MM-dd HH:mm:ss");
        } catch (Exception e) {
            e.printStackTrace();
            return time;
        }
    }

    // 将"yyyy-MM-dd'T'HH:mm:ss'Z'"格式字符串解析成东八区时间
    public static Date parseDateTZ(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        formatter.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        try {
            return formatter.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
